﻿using Ch.Admin.Bfs.Sms.Sdmx.Extension.CodeBook.Meta;
using PluginInterface;
using SdmxMl.Manager;
using SdmxMl.Module;
using System;
using System.Windows.Forms;

namespace bfs.sms.sdmx.util
{
    /// <summary>
    /// SAE plug-in for Excel variable catalog user interface
    /// </summary>
    public partial class UcExcelCatalog : UserControl, IPlugin
    {
        #region Constructor

        public UcExcelCatalog()
        {
            InitializeComponent();

            // Menu bar stretched and redefine button function disabled
            this.commandBarStripElement1.StretchHorizontally = true;
            this.commandBarStripElement1.OverflowButton.Visibility = Telerik.WinControls.ElementVisibility.Collapsed;
        }
        #endregion

        #region Plug-in Load Event

        private void UcExcelCatalog_Load(object sender, EventArgs e)
        {
            ReferenceManager.TreeMgr.LanguageChange += TreeMgr_LanguageChange;
        }

        void TreeMgr_LanguageChange(object sender, EventArgs e)
        {
            ucVarCatalog.Manager.UpdateTreeText();
        }

        #endregion

        #region IPlugin Members

        /// <summary>Event is part of interface but not used </summary>
        public event PluginEvent PluginEventExposed
        {
            add { }
            remove { }
        }


        public void Init(System.Xml.XmlDocument xDoc, object obExtend)
        {
        }

        public UserControl VisualComponent()
        {
            return this;
        }

        public bool RequiresInit
        {
            get { return false; }
        }

        #endregion

        #region Load from registry

        private void commandBarButtonLoad_Click(object sender, EventArgs e)
        {
            try
            {
                using (new SdmxMl.Helper.CWaitCursor())
                {
                    ucVarCatalog.Manager.RefreshCache();
                    ucVarCatalog.Manager.LoadMeta(TableOfContents.Catalog);
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion

        #region Excel Catalog Generation

        private void commandBarButtonExcelGenerate_Click(object sender, EventArgs e)
        {
            try
            {
                using (new SdmxMl.Helper.CWaitCursor())
                {
                    ucVarCatalog.Manager.RefreshCache();
                    ucVarCatalog.Manager.GenerateExcel();
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion

        #region SAS Format Generation

        private void radCommandBarSasFormat_Click(object sender, EventArgs e)
        {
            try
            {
                using (new SdmxMl.Helper.CWaitCursor())
                {
                    ucVarCatalog.Manager.GenerateSasFormat();
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion

        #region Add From Tree

        private void commandBarButtonGetFromTree_Click(object sender, EventArgs e)
        {
            try
            {
                ucVarCatalog.Manager.AddModuleFromMainTree();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }



        #endregion

        #region Load Set from file

        private void commandBarButtonLoadCfg_Click(object sender, EventArgs e)
        {
            OpenFileDialog LoadFileDialog = new OpenFileDialog();
            try
            {

                LoadFileDialog.Filter = "Set |*.xml";
                if (LoadFileDialog.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                {
                    ucVarCatalog.Manager.LoadCurrentSet(LoadFileDialog.FileName);
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion

        #region Save current selection in Set file

        private void commandBarButtonSaveCfg_Click(object sender, EventArgs e)
        {
            try
            {
                saveFileDialog.Filter = "Set |*.xml";
                saveFileDialog.FileName = string.Empty;
                if (saveFileDialog.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                {
                    ucVarCatalog.Manager.SaveCurrentSet(saveFileDialog.FileName);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion

    }
}
